﻿Public Class Region

    Public Cells As New List(Of Cell)

    Dim m_Hint As String
    Property Hint As String
        Get
            Return m_Hint
        End Get
        Set(ByVal Value As String)
            m_Hint = Value
            If Not First Is Nothing Then First.Invalidate()
        End Set
    End Property

    Sub Draw()
        For Each Cl In Cells
            Dim Cell = Cl
            Cell.Puzzle.DrawBorders(Cell,
                (From c In Cells Where c.x = Cell.x - 1 And c.y = Cell.y).Any,
                (From c In Cells Where c.x = Cell.x And c.y = Cell.y - 1).Any,
                (From c In Cells Where c.x = Cell.x + 1 And c.y = Cell.y).Any,
                (From c In Cells Where c.x = Cell.x And c.y = Cell.y + 1).Any)
        Next
        If Hint <> "" Then
            First.Puzzle.DrawGivenRegion(First, Hint)
        End If
    End Sub

    ReadOnly Property First As Cell
        Get
            Return (From c In Cells Order By c.i).FirstOrDefault
        End Get
    End Property

    Sub Save(ByVal Doc As Xml.XmlTextWriter)
        Doc.WriteStartElement("Region")
        Doc.WriteElementString("hint", Hint)
        Doc.WriteStartElement("Cells")
        For Each Cell In Cells
            Doc.WriteStartElement("Cell")
            Doc.WriteElementString("x", Cell.x)
            Doc.WriteElementString("y", Cell.y)
            Doc.WriteEndElement()
        Next
        Doc.WriteEndElement()
        Doc.WriteEndElement()
    End Sub

End Class